//
//  TeachTopSelectedView.m
//  SanYiTeaching
//
//  Created by 李文强 on 2019/4/8.
//  Copyright © 2019 李文强. All rights reserved.
//

#import "TeachTopSelectedView.h"

@implementation TeachTopSelectedView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame{
    if ([super initWithFrame:frame]) {
        [self initSubView];
    }
    return self;
}
- (void)initSubView{
    self.backgroundColor = MAINBACKCOLOR;
    [self addSubview:self.leftButton];
    [self addSubview:self.rightButton];
    [self addSubview:self.timeButton];
    [self addSubview:self.nianjiButton];
//    _leftButton.frame = CGRectMake(153*fw, 15, 90, 15);
    [_leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0).offset(153*fw);
        make.centerY.equalTo(self.mas_centerY);
        make.width.equalTo(@90);
        make.height.equalTo(@15);
    }];
//    _rightButton.frame = CGRectMake(_leftButton.frame.size.width+_leftButton.frame.origin.x+120*fw, 15, 90, 15);
    [_rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.leftButton.mas_right).offset(+120*fw);
        make.centerY.equalTo(self.mas_centerY);
        make.width.equalTo(@90);
        make.height.equalTo(@15);
    }];
    [_timeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_right).offset(-230);
        make.centerY.equalTo(self.rightButton.mas_centerY);
        make.width.equalTo(@100);
        make.height.equalTo(@20);
    }];
    [_nianjiButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.timeButton.mas_right).offset(5*fw);
        make.centerY.equalTo(self.rightButton.mas_centerY);
        make.width.equalTo(@100);
        make.height.equalTo(@20);
    }];
    self.Type = NO;
    _leftButton.selected = YES;
    _lineView = [[UIView alloc]init];
    _lineView.backgroundColor = [UIColorChange colorwithHexString:@"#20B097"];
    _lineView.frame = CGRectMake(0, 0, 80, 2);
    _lineView.center = CGPointMake(_leftButton.center.x, _leftButton.center.y+15);
    [self addSubview:self.lineView];
    [_lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0).offset(153*fw);
        make.bottom.equalTo(self.mas_bottom);
        make.width.equalTo(@90);
        make.height.equalTo(@2);
    }];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeView:) name:@"ChangeCellSpaNotice" object:nil];
}
/*
 这里没有把两个按钮作为移动标准，从新计算了左边
 之前以按钮为基准量约束后出现按钮跟线移动，原因不明
 目前以父类的View为约束的标准
 */
- (void)changeView:(NSNotification *)info{
    BOOL type = [[info.userInfo objectForKey:@"Type"] intValue];
    self.Type = type;
    if (type == YES) {
        //View变大
        [_leftButton mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@0).offset(180*fw);
            make.centerY.equalTo(self.mas_centerY);
            make.width.equalTo(@90);
            make.height.equalTo(@15);
        }];
        [_rightButton mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.leftButton.mas_right).offset(150*fw);
            make.centerY.equalTo(self.mas_centerY);
            make.width.equalTo(@90);
            make.height.equalTo(@15);
        }];
        //矫正l线条位置
        if (_leftButton.selected == YES) {
            [_lineView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(@0).offset(180*fw);
            }];
        }else{
            [_lineView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(@0).offset(180*fw+90+150*fw);
            }];
        }
    }else{
        //View变小
        [_leftButton mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@0).offset(153*fw);
            make.centerY.equalTo(self.mas_centerY);
            make.width.equalTo(@90);
            make.height.equalTo(@15);
        }];
        [_rightButton mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.leftButton.mas_right).offset(120*fw);
            make.centerY.equalTo(self.mas_centerY);
            make.width.equalTo(@90);
            make.height.equalTo(@15);
        }];
        //矫正l线条位置
        if (_leftButton.selected == YES) {
            [_lineView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(@0).offset(153*fw);
            }];
        }else{
            [_lineView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(@0).offset(153*fw+90+120*fw);
            }];
        }
    }
   
}

- (UIButton *)leftButton{
    if (!_leftButton) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:@"未上课时" forState:UIControlStateNormal];
        [button setTitleColor:[UIColor colorWithHexString:@"#20B097"] forState:UIControlStateSelected];
        [button setTitleColor:TITLECOLOR forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:16];
        [button addTarget:self action:@selector(noClass:) forControlEvents:UIControlEventTouchUpInside];
        _leftButton = button;
    }
    return _leftButton;
}
- (void)noClass:(UIButton *)sender{
    _currenButton = sender;
    _rightButton.selected = NO;
    sender.selected = YES;
    if (self.Type == YES) {
        [_lineView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@0).offset(180*fw);
        }];
    }else{
        [_lineView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@0).offset(153*fw);
        }];
    }
    
    //未上课时通知
    [[NSNotificationCenter defaultCenter]postNotificationName:@"WeiShangKeShiNotice" object:nil];
}
- (UIButton *)rightButton{
    if (!_rightButton) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:@"已上课时" forState:UIControlStateNormal];
        [button setTitleColor:[UIColor colorWithHexString:@"#20B097"] forState:UIControlStateSelected];
        [button setTitleColor:TITLECOLOR forState:UIControlStateNormal];
        [button addTarget:self action:@selector(haveClass:) forControlEvents:UIControlEventTouchUpInside];
        button.titleLabel.font = [UIFont systemFontOfSize:16];
        _rightButton = button;
    }
    return _rightButton;
}
- (void)haveClass:(UIButton *)sender{
    _currenButton = sender;
    _leftButton.selected = NO;
    sender.selected = YES;
    if (self.Type == YES) {
        [_lineView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@0).offset(180*fw+90+150*fw);

        }];
    }else{
        [_lineView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@0).offset(153*fw+90+120*fw);

        }];
    }

//一上课时通知
    [[NSNotificationCenter defaultCenter]postNotificationName:@"YiShangKeShiNotice" object:nil];
}
- (MinButtonViewBtn *)timeButton{
    if (!_timeButton) {
        MinButtonViewBtn *btn = [[MinButtonViewBtn alloc]initWithFrame:CGRectMake(550*fw, 15, 100, 20) andType:ButtonImageInRight];
        btn.bottomLabel.text = [PublickFunctionObject getTimeCurrent];
        btn.bottomLabel.font = [UIFont systemFontOfSize:12];
        btn.bottomLabel.textColor =TITLECOLOR;
        btn.topImage.image = [UIImage imageNamed:@"arrow_down"];
        [btn addTarget:self action:@selector(timeClick:) forControlEvents:UIControlEventTouchUpInside];
        [btn.bottomLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@0);
            make.right.equalTo(@0).offset(-15);
        }];
        _timeButton = btn;
    }
    return _timeButton;
}
- (void)timeClick:(MinButtonViewBtn *)sender{
    [self.delegate leftButtonClick];
}
- (MinButtonViewBtn *)nianjiButton{
    if (!_nianjiButton) {
        MinButtonViewBtn *btn = [[MinButtonViewBtn alloc]initWithFrame:CGRectMake(550*fw, 15, 69, 20) andType:ButtonImageInRight];
        btn.bottomLabel.text = @"选择班级";
        btn.bottomLabel.font = [UIFont systemFontOfSize:12];
        btn.bottomLabel.textColor =TITLECOLOR;
        btn.topImage.image = [UIImage imageNamed:@"arrow_down"];
        [btn addTarget:self action:@selector(nianjiButton:) forControlEvents:UIControlEventTouchUpInside];
        [btn.bottomLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@0);
            make.right.equalTo(@0).offset(-15);
        }];
        _nianjiButton = btn;
    }
    return _nianjiButton;
}
- (void)nianjiButton:(MinButtonViewBtn *)sender{
    [self.delegate rightButtonClick];
}
@end
